package com.pan.admin.module.loanProduct.service.impl;

import com.gitee.apanlh.util.date.DateUtils;
import com.pan.admin.module.channel.dao.LoanChannelDao;
import com.pan.admin.module.loanProduct.dao.LoanProductApplyFlowDao;
import com.pan.admin.module.loanProduct.dao.LoanProductDao;
import com.pan.admin.module.loanProduct.dao.LoanProductPlanDao;
import com.pan.admin.module.loanProduct.entity.po.LoanProductApplyFlowPO;
import com.pan.admin.module.loanProduct.entity.qo.LoanProductApplyFlowQO;
import com.pan.admin.module.loanProduct.service.LoanProductApplyFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 机构流程日志表
 *
 * @author caoxin
 */
@Service
public class LoanProductApplyFlowServiceImpl implements LoanProductApplyFlowService {

    @Autowired
    private LoanProductApplyFlowDao loanProductApplyFlowDao;
    @Autowired
    private LoanChannelDao loanChannelDao;
    @Autowired
    private LoanProductDao loanProductDao;
    @Autowired
    private LoanProductPlanDao loanProductPlanDao;

    @Override
    @Transactional(readOnly = true)
    public List<LoanProductApplyFlowPO> list(LoanProductApplyFlowQO loanProductApplyFlowQO) {
        List<LoanProductApplyFlowPO> list = this.loanProductApplyFlowDao.list(loanProductApplyFlowQO);

        list.forEach(item -> {
            Long loanChannelId = item.getLoanChannelId();
            if (loanChannelId == null) {
                return;
            }
            String loanChannelName = this.loanChannelDao.findLoanChannelNameByLoanChannelId(loanChannelId);
            item.setLoanChannelName(loanChannelName);
        });

        list.forEach(item -> {
            Long loanProductId = item.getLoanProductId();
            if (loanProductId == null) {
                return;
            }
            String LoanProductName = this.loanProductDao.findLoanProductNameByLoanProductId(loanProductId);
            item.setLoanProductName(LoanProductName);
        });

        list.forEach(item -> {
            Long loanProductPlanId = item.getLoanProductPlanId();
            if (loanProductPlanId == null) {
                return;
            }
            String loanProductPlanName = this.loanProductPlanDao.findLoanProductPlanNameByLoanProductPlanId(loanProductPlanId);
            item.setLoanProductPlanName(loanProductPlanName);
        });
        return list;
    }

    /**
     * 清理流程日志表
     */
    @Override
    public void clean() {
        //  只保留昨天全天日志
        String endTime = DateUtils.dateTimeOffsetByDay(1);
        //  批次
        int limitSize = 1000;

        int count = loanProductApplyFlowDao.cleanCount(endTime);
        int cleanRow = 0;

        while (cleanRow < count) {
            int row = loanProductApplyFlowDao.clean(limitSize, endTime);

            //  清理小于退出
            if (row < limitSize || row <= 0) {
                break;
            }

            cleanRow += row;
        }
    }
}
